package com.xhui.vimarket.zkplus.zul.attr.mod.impl;t java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.directwebremoting.util.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listhead;
import org.zkoss.zul.Listheader;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Toolbarbutton;

import com.xhui.crm.common.exception.RtManagerException;
import com.xhui.crm.common.util.numeric.NumericUtil;
import com.xhui.crm.common.util.string.StrUtil;
import com.xhui.crm.common.vo.RetVO;com.xhui.vimarket.zkplusui.vimarket.zkplus.util.Compcom.xhui.vimarket.zkplusrt com.xhui.vimarket.zkpcom.xhui.vimarket.zkplusls;
import com.xhui.vicom.xhui.vimarket.zkplusul.ErrorBox;
import com.xhcom.xhui.vimarket.zkpluslus.zul.attr.AttrExt;
import comcom.xhui.vimarket.zkpluszkplus.zul.attr.bean.AttrInst;
icom.xhui.vimarket.zkplusvimarket.zkplus.zul.attr.bean.Acom.xhui.vimarket.zkplust com.xhui.vimarket.zkplus.zul.attr.bean.Vcom.xhui.vimarket.zkpluscom.xhui.vimarket.zkplus.zul.attr.bean.imcom.xhui.vimarket.zkplus;
import com.xhui.vimarket.zkplus.zul.com.xhui.vimarket.zkplusmpConstants;
import com.xhui.vimarket.zkplus.zul.attr.mod.AttrCompFaccom.xhui.vimarket.zkplusom.xhui.vimarket.zkplus.zul.attr.mod.AttrCompUtil;

/**
 * @版权：福富软件 版权所有 (c) 2010
 * @文件：com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrMultiSubComp.java
 * @所含类：
 * @author: wuyx
 * @version: V1.0
 * @see:
 * @创建日期：2010-12-20
 * @功能说明：属性控件接口
 * @修改记录： =============================================================<br>
 *        日期:2010-12-20 创建
 *        =============================================================<br>
 */
public final class AttrMultiSubComp extends AttrBaseComp {
    
    /**
     * 实例.
     */
    private static AttrMultiSubComp inst   = new AttrMultiSubComp();
    
    /**
     * 日志.
     */
    private static Logger           logger = Logger.getLogger(AttrMultiSubComp.class);
    
    /**
     * 构造函数. 
     */
    private AttrMultiSubComp() {
    }
    
    private int max = -1;
    
    private int min = -1;
    
    /**
     * 获取实例.
     * 
     * @return AttrBaseComp
     */
    public static AttrMultiSubComp instance() {
        return AttrMultiSubComp.inst;
    }
    
    /**
     * 生成组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件模板
     * @param comp 显示容器组件
     */
    @Override
    public void create(final AttrExt attrExt, final AttrTmpl attrTmpl, final Component comp) {
        super.create(attrExt, attrTmpl, comp);
        // panel
        final Hbox chbox = new Hbox();
        chbox.setAttribute(AttrBaseComp.COMP_IDX, attrTmpl.getIdx());
        chbox.setAttribute(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
        chbox.setAttribute(AttrBaseComp.COMP_ATTRID, attrTmpl.getAttrId());
        chbox.setAttribute(AttrBaseComp.COMP_PARAM1, attrTmpl.getParam1());
        //设置多实例最大和最小值
        if (!StrUtil.isNullOrEmpty(attrTmpl.getParam1())) {
            String[] params = attrTmpl.getParam1().split(AttrBaseComp.SPLIT_FLAG);
            if (params.length > 1) {
                this.min = NumericUtil.toInt(params[0]);
                this.max = NumericUtil.toInt(params[1]);
            }
        }
        chbox.setWidth("100%");
        //final Listbox listbox = new Listbox();
        final Listbox listbox = (Listbox) attrExt.getAttrAda().getAttrTmplCompStr(
            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null, chbox);
        listbox.setWidth("99%");
        final Listhead listhead = new Listhead();
        listhead.setWidth("100%");
        final Listitem listitem = new Listitem();
        final int mgroupId = attrExt.getGroupId();
        listitem.setAttribute(AttrBaseComp.COMP_GROUPID, mgroupId);
        listitem.setWidth("100%");
        final Listheader listheadera = new Listheader();
        listheadera.setWidth("98%");
        final Listheader listheaderb = new Listheader();
        listheaderb.setWidth("38px");
        final Listcell listcella = new Listcell();
        final Listcell listcellb = new Listcell();
        // add to zul
        chbox.setParent(comp);
        listbox.setParent(chbox);
        listhead.setParent(listbox);
        listitem.setParent(listbox);
        listheadera.setParent(listhead);
        listheaderb.setParent(listhead);
        listcella.setParent(listitem);
        listcellb.setParent(listitem);
        // =- button
       com.xhui.vimarket.zkplustbtna = new Toolbarbutton();
        tbtna.setLabel("+");
        ComposerUtil.bindComposer(tbtna, "com.xhui.vimarket.zkplus.zul.attr.evt.multi.AddBtnComposer",
            listcellb);
    com.xhui.vimarket.zkplusxecutions
        //            .createComponentsDirectly(
        //                "<toolbarbutton apply=\"com.xhui.vimarket.zkplus.zul.attr.evt.multi.AddBtnComposer\" label=\"+\" />",
        //                null, listcellb, nulcom.xhui.vimarket.zkplus        Executions
        //            .createComponentsDirectly(
        //                "<toolbarbutton apply=\"com.xhui.vimarket.zkplus.zul.attr.evt.multi.DelBtnComposer\" label=\"-\" />",
        //                null,com.xhui.vimarket.zkplusl);
        Toolbarbutton tbtnb = new Toolbarbutton();
        tbtnb.setLabel("-");
        ComposerUtil.bindComposer(tbtnb, "com.xhui.vimarket.zkplus.zul.attr.evt.multi.DelBtnComposer",
            listcellb);
        // comp log
        if (AttrMultiSubComp.logger.isDebugEnabled()) {
            attrExt.appendInnerComp("<hbox width=\"100%\">");
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_ATTRID, "" + attrTmpl.getAttrId());
            attrExt.appendInnerComp("<listbox width=\"99%\"");
            attrExt.appendInnerComp(">");
            attrExt.appendInnerComp("<listhead width=\"100%\">");
            attrExt.appendInnerComp("<listheader />");
            attrExt.appendInnerComp("<listheader />");
            attrExt.appendInnerComp("</listhead>");
            attrExt.appendInnerComp("<listitem width=\"100%\">");
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_GROUPID, "" + mgroupId);
            attrExt.appendInnerComp("<listcell>");
        }
        
        // sub comp
        final int[] maxxy = AttrBaseComp.getMaxxy(attrExt, attrTmpl);
        final StringBuffer sbuf = new StringBuffer();
        if (maxxy[0] < 2) {
            sbuf.append("100%");
        } else {
            final int onew = 100 / maxxy[0];
            sbuf.append(onew);
            sbuf.append("%");
            for (int i = 1; i < maxxy[0]; i++) {
                sbuf.append(",");
                sbuf.append(onew);
                sbuf.append("%");
            }
        }
        AttrMultiSubComp.logger.debug(maxxy[0] + "-" + maxxy[1]);
        final int sum = attrTmpl.getAttrTmplListCount();
        int line = -1;
        // Hbox
        Hbox hbox = null;
        /*
         * = new Hbox(); hbox.setWidth("100%"); hbox.setWidths(sbuf.toString());
         * hbox.setParent(panelchildren);
         */
        AttrMultiSubComp.logger.debug("hbox");
        for (int i = 0; i < sum; i++) {
            final AttrTmpl subAttrTmpl = attrTmpl.getAttrTmplList(i);
            if (AttrMultiSubComp.logger.isDebugEnabled()) {
                AttrMultiSubComp.logger.debug("x:" + subAttrTmpl.getXAddr() + ",y:"
                    + subAttrTmpl.getYAddr() + ",widths:" + sbuf.toString());
            }
            if (line != subAttrTmpl.getYAddr()) {
                // if (line != -1) {
                hbox = new Hbox();
                hbox.setWidth("100%");
                hbox.setWidths(sbuf.toString());
                hbox.setParent(listcella);
                AttrMultiSubComp.logger.debug("hbox");
                // }
                // comp log
                if (AttrMultiSubComp.logger.isDebugEnabled()) {
                    if (line != -1) {
                        attrExt.appendInnerComp("</hbox>");
                    }
                    attrExt.appendInnerComp("<hbox width=\"100%\" widths=\"");
                    attrExt.appendInnerComp(sbuf.toString());
                    attrExt.appendInnerComp("\">");
                }
                line = subAttrTmpl.getYAddr();
            }
            AttrCompFactory.getInst(subAttrTmpl.getDisplayMode())
                .create(attrExt, subAttrTmpl, hbox);
        }
        
        if (AttrMultiSubComp.logger.isDebugEnabled()) {
            attrExt.appendInnerComp("</hbox>");
            attrExt.appendInnerComp("</listcell>");
            attrExt.appendInnerComp("<listcell>+-</listcell>");
            attrExt.appendInnerComp("</listitem>");
            attrExt.appendInnerComp("</listbox>");
            attrExt.appendInnerComp("</hbox>");
        }
        
        super.afterCreate(attrExt, attrTmpl, comp);
    }
    
    /**
     * 设置值.
     * 多实例，暂时只支持一个下级情况，并且是文本类型的
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param attrInstList 属性实例
     * @param attrViewMap attrViewMap
     */
    @Override
    public void setValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList, final Map<String, Integer> attrViewMap) {
        super.setValue(attrExt, comp, attrInstList, attrViewMap);
        // 计算下级采集了几条实例数据
        //// 根据界面配置获取下级的数据最大数量，暂时先按只有一个下级控件处理！
        final Listbox listbox = (Listbox) comp.getFirstChild();
        final List<Component> listitemList = listbox.getItems();
        // 第一个Listitem
        final Component listitemFir = listitemList.get(0);
        final Component subRoot = listitemFir.getFirstChild().getFirstChild().getFirstChild();
        final long theSubAttrId = NumUtils.toLong(""
            + subRoot.getAttribute(AttrBaseComp.COMP_ATTRID));
        ArrayList<AttrInst> theSubAttrList = AttrBaseComp.getAttrInstListById(attrInstList,
            theSubAttrId);
        // 底层返回会将多实例值按照AttrCompConstants.ATTR_VALUES_SEPARATOR分格的字符串
        if (theSubAttrList != null && theSubAttrList.size() > 0) {
            String v = theSubAttrList.get(0).getParam1();
            if (theSubAttrList.get(0).getSubAttrInsts() != null
                && theSubAttrList.get(0).getSubAttrInsts().size() > 0) {
                theSubAttrList = (ArrayList) theSubAttrList.get(0).getSubAttrInsts();
            } else if (v != null && v.indexOf(AttrCompConstants.ATTR_VALUES_SEPARATOR) > -1) {
                String[] vals = v.split(AttrCompConstants.ATTR_VALUES_SEPARATOR);
                int vSum = vals.length;
                ArrayList<AttrInst> tAttrInsts = new ArrayList<AttrInst>();
                for (int i = 0; i < vSum; i++) {
                    AttrInst tInst = new AttrInstImpl();
                    tInst.copyFrom(theSubAttrList.get(0));
                    tInst.setParam1(vals[i]);
                    tAttrInsts.add(tInst);
                }
                theSubAttrList = tAttrInsts;
            }
        }
        // 底层适应性改造End
        final int sumA = theSubAttrList.size();
        
        // 根据采集实例数据量，扩展生成界面，并设置分组（按照序号设置）
        if (sumA >= 1) {
            // hbox//listbox//listitem//listcell//hbox
            final int listItemSum = listitemList == null ? 0
                : listitemList.size();
            // 获取模板定义
            final AttrTmpl mltiAttrTmpl = AttrCompUtil.getEqualAttrTmpl(attrExt.getAttrSpec()
                .getAttrTmpl(), comp);
            if (mltiAttrTmpl == null) {
                throw new RtManagerException("多实例控件，获取模板数据为空", this.getClass(), "onClick",
                    "multiAttrTmplNull");
            }
            // 生成总数-1
            if (sumA > 1) {
                for (int i = 1; i < sumA; i++) {
                    this.doAddLine(attrExt, mltiAttrTmpl, (Listitem) listitemFir);
                }
            }
            // 绑定数据
            // hbox//listbox//listitem//listcell//hbox
            final int sum = listitemList == null ? 0
                : listitemList.size();
            for (int i = 0; i < sum; i++) { // listitem
                // sub
                final Component listitem = listitemList.get(i);
                final List<Component> subBoxList = listitem.getFirstChild().getChildren();
                final int jsum = subBoxList == null ? 0
                    : subBoxList.size();
                for (int j = 0; j < jsum; j++) { // listcell
                    final Component subBox = subBoxList.get(j);
                    final List<Component> subCompRootBoxList = subBox.getChildren();
                    final int ksum = subCompRootBoxList == null ? 0
                        : subCompRootBoxList.size();
                    // 当前实例值
                    final Component subCompRootBoxFir = subCompRootBoxList.get(0);
                    final long thisSubAttrId = NumUtils.toLong(""
                        + subCompRootBoxFir.getAttribute(AttrBaseComp.COMP_ATTRID));
                    ArrayList<AttrInst> thisSubAttrInstList = AttrBaseComp.getAttrInstListById(
                        attrInstList, thisSubAttrId);
                    // 底层返回会将多实例值按照AttrCompConstants.ATTR_VALUES_SEPARATOR分格的字符串
                    if (thisSubAttrInstList != null && thisSubAttrInstList.size() > 0
                        && thisSubAttrInstList.get(0).getSubAttrInsts() != null
                        && thisSubAttrInstList.get(0).getSubAttrInsts().size() > 0) {
                        thisSubAttrInstList = (ArrayList) thisSubAttrInstList.get(0)
                            .getSubAttrInsts();
                    } else if (thisSubAttrInstList != null && thisSubAttrInstList.size() > 0) {
                        String v = thisSubAttrInstList.get(0).getParam1();
                        if (v != null && v.indexOf(AttrCompConstants.ATTR_VALUES_SEPARATOR) > -1) {
                            String[] vals = v.split(AttrCompConstants.ATTR_VALUES_SEPARATOR);
                            int vSum = vals.length;
                            ArrayList<AttrInst> tAttrInsts = new ArrayList<AttrInst>();
                            for (int mm = 0; mm < vSum; mm++) {
                                AttrInst tInst = new AttrInstImpl();
                                tInst.copyFrom(theSubAttrList.get(0));
                                tInst.setParam1(vals[mm]);
                                tAttrInsts.add(tInst);
                            }
                            thisSubAttrInstList = tAttrInsts;
                        }
                    }
                    // 底层适应性改造End
                    for (int k = 0; k < ksum; k++) { // sub comp
                        final Component subCompRootBox = subCompRootBoxList.get(k);
                        if (subCompRootBox != null) {
                            ArrayList<AttrInst> thisSubAttrInstListOut = new ArrayList<AttrInst>();
                            thisSubAttrInstListOut.add(thisSubAttrInstList.get(i));
                            AttrCompFactory.getInstByComponent(subCompRootBox).setValue(attrExt,
                                subCompRootBox, thisSubAttrInstListOut, attrViewMap);
                        }
                    }
                }
            }
        }
    }
    
    /**
     * 设置值.
     * 按照多实例控件实例化处理，暂时不用该方式
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param attrInstList 属性实例
     * @param attrViewMap attrViewMap
     */
    public void setValueOra(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList, final Map<String, Integer> attrViewMap) {
        super.setValue(attrExt, comp, attrInstList, attrViewMap);
        // 计算下级采集了几条实例数据
        final long theAttrId = NumUtils.toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID));
        final ArrayList<AttrInst> theAttrList = AttrBaseComp.getAttrInstListById(attrInstList,
            theAttrId);
        final int sumA = theAttrList.size();
        
        // 根据采集实例数据量，扩展生成界面，并设置分组（按照序号设置）
        if (sumA >= 1) {
            // hbox//listbox//listitem//listcell//hbox
            final List<Component> listitemListA = comp.getFirstChild().getChildren();
            final int listItemSum = listitemListA == null ? 0
                : listitemListA.size();
            // 获取模板定义
            final AttrTmpl mltiAttrTmpl = AttrCompUtil.getEqualAttrTmpl(attrExt.getAttrSpec()
                .getAttrTmpl(), comp);
            if (mltiAttrTmpl == null) {
                throw new RtManagerException("多实例控件，获取模板数据为空", this.getClass(), "onClick",
                    "multiAttrTmplNull");
            }
            // 第一个Listitem
            final Component listitemA = listitemListA.get(0);
            // 生成总数-1
            if (sumA > 1) {
                for (int i = 1; i < sumA; i++) {
                    this.doAddLine(attrExt, mltiAttrTmpl, (Listitem) listitemA);
                }
            }
            // 绑定数据
            // hbox//listbox//listitem//listcell//hbox
            final List<Component> listitemList = comp.getFirstChild().getChildren();
            final int sum = listitemList == null ? 0
                : listitemList.size();
            for (int i = 0; i < sum; i++) { // listitem
                // sub
                final Component listitem = listitemList.get(i);
                final List<Component> subBoxList = listitem.getFirstChild().getChildren();
                final int jsum = subBoxList == null ? 0
                    : subBoxList.size();
                for (int j = 0; j < jsum; j++) { // listcell
                    final Component subBox = subBoxList.get(j);
                    final List<Component> subCompRootBoxList = subBox.getChildren();
                    final int ksum = subCompRootBoxList == null ? 0
                        : subCompRootBoxList.size();
                    for (int k = 0; k < ksum; k++) { // sub comp
                        final Component subCompRootBox = subCompRootBoxList.get(k);
                        if (subCompRootBox != null) {
                            AttrCompFactory.getInstByComponent(subCompRootBox).setValue(attrExt,
                                subCompRootBox, theAttrList.get(0).getSubAttrInsts(), attrViewMap);
                        }
                    }
                }
            }
        }
    }
    
    /**
     * 获取值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getValue(final AttrExt attrExt, final Component comp) {
        super.getValue(attrExt, comp);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        // 自身要实例化，取下级数据,对下级设置mgroupId
        // hbox//listbox//listitem//listcell//hbox
        final List<Component> listitemList = comp.getFirstChild().getChildren();
        final int sum = listitemList == null ? 0
            : listitemList.size();
        for (int i = 0; i < sum; i++) { // listitem
            // multi attrInst
            final AttrInst attrInst = attrExt.newAttrInst();
            attrInst.setAttrId(NumUtils.toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID)));
            attrInst.setJavaCode(StrUtil.strnull(comp.getAttribute(AttrBaseComp.COMP_JAVACODE)));
            attrInst.setParam1("");
            attrInst.setOp(this.getToolbarOp(comp));
            retList.add(attrInst);
            final ArrayList<AttrInst> msubRetList = new ArrayList<AttrInst>();
            // sub
            final Component listitem = listitemList.get(i);
            final int groupId = NumUtils.toInt(""
                + listitem.getAttribute(AttrBaseComp.COMP_GROUPID));
            final List<Component> subBoxList = listitem.getFirstChild().getChildren();
            final int jsum = subBoxList == null ? 0
                : subBoxList.size();
            for (int j = 0; j < jsum; j++) { // listcell
                final Component subBox = subBoxList.get(j);
                final List<Component> subCompRootBoxList = subBox.getChildren();
                final int ksum = subCompRootBoxList == null ? 0
                    : subCompRootBoxList.size();
                for (int k = 0; k < ksum; k++) { // sub comp
                    final Component subCompRootBox = subCompRootBoxList.get(k);
                    if (subCompRootBox != null) {
                        final ArrayList<AttrInst> subRetList = AttrCompFactory.getInstByComponent(
                            subCompRootBox).getValue(attrExt, subCompRootBox);
                        if (subRetList != null) {
                            final int msum = subRetList.size();
                            for (int m = 0; m < msum; m++) {
                                subRetList.get(m).setMgroupId(groupId);
                            }
                            msubRetList.addAll(subRetList);
                        }
                    }
                }
            }
            attrInst.setSubAttrInsts(msubRetList);
        }
        // 界面处理：多实例控件实例化，并且取值在控件实例化下级保持
        // 底层存储：多实例控件不实例化，底层通过设置数组方式进行保存
        // 进行适应底层转换
        final ArrayList<AttrInst> retList2 = new ArrayList<AttrInst>();
        if (retList != null) {
            for (AttrInst tinst : retList) {
                if (tinst.getSubAttrInsts() != null) {
                    retList2.addAll(tinst.getSubAttrInsts());
                }
com.xhui.vimarket.zkplus        }
        // 转换结束
        return recom.xhui.vimarket.zkplus  // return retList;
    }
    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp#signRuleHint(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2012-2-16 wuyx
     */
    @Override
    public ArrayList<ValiVo> signRuleHint(AttrExt attrExt, Component comp) {
        super.signRuleHint(attrExt, comp);
        if (AttrCompUtil.isCantNull(comp)) { // 必填
            ArrayList<AttrInst> valList = this.getValue(attrExt, comp);
            if (valList == null || valList.size() < 1) {
                ValiVo retVo = new ValiVo();
                ArrayList<ValiVo> retVoList = new ArrayList<ValiVo>();
                retVoList.add(retVo);
                return retVoList;
            }
        }
        return null;
    }
    
    /**
     * 取空值属性实例.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 模板
     */
    @Override
    public ArrayList<AttrInst> getNullValue(final AttrExt attrExt, final AttrTmpl attrTmpl) {
        super.getNullValue(attrExt, attrTmpl);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        
        final int sum = attrTmpl.getAttrTmplListCount();
        AttrMultiSubComp.logger.debug("subRowList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final AttrTmpl subAttrTmpl = attrTmpl.getAttrTmplList(i);
            final AttrInst attrInst = attrExt.newAttrInst();
            attrInst.setAttrId(subAttrTmpl.getAttrId());
            attrInst.setJavaCode(subAttrTmpl.getJavaCode());
            retList.add(attrInst);
        }
        return retList;
    }
    
    /**
     * 生成组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件模板
     * @param item 容器组件行
     */
    public void doAddLine(final AttrExt attrExt, final AttrTmpl attrTmpl, final Listitem item) {
        // listbox
        final Listbox listbox = (Listbox) item.getParent();
        if (this.max != -1 && listbox.getItemCount() >= this.max) {
            ErrorBox.newInstance().showError(item.getParent(),
                "实例数量已经达到最大值[" + this.max + "]不能再增加，请确认！");
            // throw new RtManagerException("实例数量已经达到最大值[" + this.max + "]不能再增加，请确认！", this.getClass(), "doAddLine",
            // "multiAttrTmplNull");
            return;
        }
        // new item
        final Listitem listitem = new Listitem();
        listitem.setWidth("100%");
        final int mgroupId = attrExt.getGroupId();
        listitem.setAttribute(AttrBaseComp.COMP_GROUPID, mgroupId);
        final Listcell listcella = new Listcell();
        final Listcell listcellb = new Listcell();
        // add to zul
        listitem.setParent(listbox);
        listcella.setParent(listitem);
        listcelcom.xhui.vimarket.zkplusstitem);
        // =- button
        Toolbarbutton tbtna = new Toolbarbutton();
        tbtna.setLabel("+");
        ComposerUtil.bindComposer(tbtna, "com.xhui.vimarket.zkplus.zul.attr.evtcom.xhui.vimarket.zkplusmposer",
            listcellb);
        //        Executions
        //            .createComponentsDirectly(
        //                "<toolbarbutton apply=\"com.xhui.vimarket.zkplus.zul.attr.evt.multi.AddBtnComposer\" label=\"+\com.xhui.vimarket.zkplus //                null, listcellb, null);
        //        Executions
        //            .createComponentsDirectly(
        //                "<toolbarbutton apply=\"com.xhui.vimarket.zkplus.zul.attr.evt.multi.DelBtnComposcom.xhui.vimarket.zkplus" />",
        //                null, listcellb, null);
        Toolbarbutton tbtnb = new Toolbarbutton();
        tbtnb.setLabel("-");
        ComposerUtil.bindComposer(tbtnb, "com.xhui.vimarket.zkplus.zul.attr.evt.multi.DelBtnComposer",
            listcellb);
        
        // sub comp
        final int[] maxxy = AttrBaseComp.getMaxxy(attrExt, attrTmpl);
        final StringBuffer sbuf = new StringBuffer();
        if (maxxy[0] < 2) {
            sbuf.append("100%");
        } else {
            final int onew = 100 / maxxy[0];
            sbuf.append(onew);
            sbuf.append("%");
            for (int i = 1; i < maxxy[0]; i++) {
                sbuf.append(",");
                sbuf.append(onew);
                sbuf.append("%");
            }
        }
        AttrMultiSubComp.logger.debug(maxxy[0] + "-" + maxxy[1]);
        final int sum = attrTmpl.getAttrTmplListCount();
        int line = -1;
        // Hbox
        Hbox hbox = null;
        /*
         * = new Hbox(); hbox.setWidth("100%"); hbox.setWidths(sbuf.toString());
         * hbox.setParent(panelchildren);
         */
        AttrMultiSubComp.logger.debug("hbox");
        for (int i = 0; i < sum; i++) {
            final AttrTmpl subAttrTmpl = attrTmpl.getAttrTmplList(i);
            if (AttrMultiSubComp.logger.isDebugEnabled()) {
                AttrMultiSubComp.logger.debug("x:" + subAttrTmpl.getXAddr() + ",y:"
                    + subAttrTmpl.getYAddr() + ",widths:" + sbuf.toString());
            }
            if (line != subAttrTmpl.getYAddr()) {
                // if (line != -1) {
                hbox = new Hbox();
                hbox.setWidth("100%");
                hbox.setWidths(sbuf.toString());
                hbox.setParent(listcella);
                AttrMultiSubComp.logger.debug("hbox");
                // }
                // comp log
                if (AttrMultiSubComp.logger.isDebugEnabled()) {
                    if (line != -1) {
                        attrExt.appendInnerComp("</hbox>");
                    }
                    attrExt.appendInnerComp("<hbox width=\"100%\" widths=\"");
                    attrExt.appendInnerComp(sbuf.toString());
                    attrExt.appendInnerComp("\">");
                }
                line = subAttrTmpl.getYAddr();
            }
            AttrCompFactory.getInst(subAttrTmpl.getDisplayMode())
                .create(attrExt, subAttrTmpl, hbox);
        }
    }
    
    /**
     * 生成组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件模板
     * @param item 容器组件行
     */
    public void doDelLine(final AttrExt attrExt, final AttrTmpl attrTmpl, final Listitem item) {
        // listbox
        final Listbox listbox = (Listbox) item.getParent();
        if (this.min != -1 && listbox.getItemCount() <= this.min) {
            ErrorBox.newInstance().showError(item.getParent(),
                "实例数量已经达到最小值[" + this.min + "]不能再删除，请确认！");
            // throw new RtManagerException("实例数量已经达到最小值[" + this.min + "]不能再删除，请确认！",
            //    this.getClass(), "doDelLine", "multiAttrTmplNull");
            return;
        }
        // remove
        if (listbox.getItemCount() > 1) {
            listbox.removeChild(item);
        }
    }
    
    /**
     * getRootCompByAddOrDelBtn.
     * 
     * @param btn Component
     * @return Hbox
     * @author wuyx
     * 2011-2-10 wuyx 
     */
    public static Hbox getRootCompByAddOrDelBtn(final Component btn) {
        return (Hbox) btn.getParent().getParent().getParent().getParent();
    }
    
    /**
     * getListitemByAddOrDelBtn.
     * 
     * @param btn Component
     * @return Listbox
     * @author wuyx
     * 2011-2-10 wuyx 
     */
    public static Listitem getListitemByAddOrDelBtn(final Component btn) {
        return (Listitem) btn.getParent().getParent();
    }
    
    /**
     * .
     * 
     * @param rootComp Component
     * @author wuyx
     * 2011-11-28 wuyx 
     */
    public static void fireChangeVal(Component rootComp) {
        Events.sendEvent("onChangeVal", rootComp.getFirstChild(), null);
    }
    
    /**
     * getSubCompAttrIds.
     * 
     * @param rootComp Component
     * @return List<Long>
     * @author wuyx
     * 2011-11-28 wuyx 
     */
    public static List<Long> getSubCompAttrIds(Component rootComp) {
        // 计算下级采集了几条实例数据
        //// 根据界面配置获取下级的数据最大数量，暂时先按只有一个下级控件处理！
        final Listbox listbox = (Listbox) rootComp.getFirstChild();
        final List<Component> listitemList = listbox.getItems();
        // 第一个Listitem
        final Component listitemFir = listitemList.get(0);
        final Component subRoot = listitemFir.getFirstChild().getFirstChild().getFirstChild();
        final long theSubAttrId = NumUtils.toLong(""
            + subRoot.getAttribute(AttrBaseComp.COMP_ATTRID));
        ArrayList<Long> subAttrIds = new ArrayList<Long>();
        subAttrIds.add(theSubAttrId);
        return subAttrIds;
    }
    
    @Override
    public List<RetVO> validate(final AttrExt attrExt, final Component comp) {
        final Listbox firstBox = (Listbox) comp.getFirstChild();
        final List<Component> listitemList = firstBox.getItems();
        final Listbox listbox = (Listbox) listitemList.get(0).getParent();
        if (this.min != -1 && listbox.getItemCount() < this.min) {
            return AttrCompUtil.getRetVoList(AttrCompConstants.RET_ERROR, "实例数量小于最小值[" + this.min
                + "]，请确认！" + AttrCompConstants.RET_ERROR, comp);
        }
        return null;
    }
}
